package com.fh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fh.mapper.UserMapper;
import com.fh.model.User;
import com.fh.service.UserService;
import com.fh.util.DataTablePageUtil;
import com.fh.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public DataTablePageUtil<User> queryUser(DataTablePageUtil<User> page, UserVo userVo) {
        //将条件查询放入json中
        JSONObject json = (JSONObject)JSONObject.toJSON(userVo);
        //这是求分页的下标
        json.put("start",page.getStart());
        json.put("length",page.getLength());
        //查询数据
        List<User> users = userMapper.queryUser(json);
        //查询数据个数
        Integer count = userMapper.queryCount();
        page.setData(users);
        page.setRecordsTotal(count);
        page.setRecordsFiltered(count);
        return page;
    }

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public User toUpdateUser(Integer userId) {
        return userMapper.toUpdateUser(userId);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUser(Integer userId) {
        userMapper.deleteUser(userId);
    }

    @Override
    public void deleteBothUser(Integer[] arr) {
        userMapper.deleteBothUser(arr);
    }
}
